﻿Messenger.options = {
    extraClasses: 'messenger-fixed messenger-on-top',
    theme: 'block'
}

$(function () {
    $('.select2').select2({
        placeholder: {
            id: '0',
            text: ''
        },
        allowClear: true,
        width: '100%'
    });

    $('#product-specifications').submit(function (e) {
        e.preventDefault();

        var data = [];

        $('#specification-groups select').each(function () {
            var me = $(this);
            var specGroupId = me.data('specgroupid');
            var specId = me.data('specid');
            var value = parseInt(me.val());
            if (value > 0) {
                data.push({
                    SpecificationGroupId: specGroupId,
                    SpecificationId: specId,
                    SpecificationValueId: value
                });
            }
        });

        $.post('ProductSpecifications', {
            id: $('#product-specifications').data('id'),
            jsonData: JSON.stringify(data)
        }, function (result) {
            switch (result.status) {
                case 200:
                    location.href = result.message;
                    break;
                default:
                    Messenger().post({
                        message: result.message,
                        type: 'error',
                        showCloseButton: true
                    });
                    break;
            }
        });
    });
})